import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
import { useHistory } from "react-router";
import axios from "axios";
// import cookie from "react-cookies";
interface UserInfoState {
  userPoint: any;
  profile: any;
  peopleCanSeeMyPlayRecord: boolean;
  pcSign: boolean;
  mobileSign: any;
  listenSongs: any;
  level: any;
  createTime: any;
  createDays: any;
  adValid: boolean;
  bindings: any;
  playlists: any;
}

const initialState: UserInfoState = {
  userPoint: "",
  profile: "",
  peopleCanSeeMyPlayRecord: false,
  pcSign: false,
  mobileSign: "",
  listenSongs: "",
  level: "",
  createTime: "",
  createDays: "",
  adValid: true,
  bindings: "",
  playlists: "",
};

export const getUserDetail = createAsyncThunk(
  "user/detail",
  async (
    paramaters: {
      id: string;
    },
    thunkAPI
  ) => {
    const { data } = await axios.post(
      `http://localhost:3000/user/detail?uid=${paramaters.id}`
    );
    return data;
  }
);
export const getPlaylist = createAsyncThunk(
  "user/playlist",
  async (
    paramaters: {
      id: string;
    },
    thunkAPI
  ) => {
    const { data } = await axios.post(
      `http://localhost:3000/user/playlist?uid=${paramaters.id}`
    );
    return data;
  }
);
export const userDetailSlice = createSlice({
  name: "userDetail",
  initialState,
  reducers: {},

  extraReducers: {
    [getUserDetail.fulfilled.type]: (s, a) => {
      s.userPoint = a.payload["userPoint"];
      s.profile = a.payload["profile"];
      s.peopleCanSeeMyPlayRecord = a.payload["peopleCanSeeMyPlayRecord"];
      s.pcSign = a.payload["pcSign"];
      s.mobileSign = a.payload["mobileSign"];
      s.listenSongs = a.payload["listenSongs"];
      s.level = a.payload["level"];
      s.createTime = a.payload["createTime"];
      s.createDays = a.payload["createDays"];
      s.adValid = a.payload["adValid"];
      s.bindings = a.payload["bindings"];
    },
    [getPlaylist.fulfilled.type]: (s, a) => {
      s.playlists = a.payload["playlist"];
    },
  },
});
